package com.charger.controller;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.domain.ChargerFinancial;
import com.charger.service.IChargerFinancialService;
import com.charger.common.utils.poi.ExcelUtil;

/**
 * 财务报表Controller
 * 
 * @author qkp
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/statistic/financial")
public class ChargerFinancialController extends BaseController
{
    @Autowired
    private IChargerFinancialService chargerFinancialService;

    /**
     * 查询财务报表列表
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:list')")
    @GetMapping("/list")
    public Object list(ChargerFinancial chargerFinancial)
    {
        startPage();
        List<ChargerFinancial> list = chargerFinancialService.selectChargerFinancialList(chargerFinancial);
        Map<String, Object> map = new HashMap<>();
        map.put("page", new PageInfo<ChargerFinancial>(list));
        List<Map> listMap = new ArrayList<Map>();
        //返回支付类型及列表中查询到的金额
        if (list != null && list.size() > 0){
            AtomicReference<Double> wechat = new AtomicReference<>(0.0);
            AtomicReference<Double> ali = new AtomicReference<>(0.0);
            AtomicReference<Double> card = new AtomicReference<>(0.0);
            AtomicReference<Double> sys = new AtomicReference<>(0.0);
            list.forEach(chargerFinancial1 -> {
                wechat.updateAndGet(v -> v + chargerFinancial1.getWechatPay());
                ali.updateAndGet(v -> v + chargerFinancial1.getAliPay());
                card.updateAndGet(v -> v + chargerFinancial1.getCardPay());
                sys.updateAndGet(v -> v + chargerFinancial1.getSysPay());
            });
            Map aliM = new HashMap();
            aliM.put("value", new BigDecimal(ali.get()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            aliM.put("name", "支付宝");
            Map wechatM = new HashMap();
            wechatM.put("value", new BigDecimal(wechat.get()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            wechatM.put("name", "微信");
            Map cardM = new HashMap();
            cardM.put("value", new BigDecimal(card.get()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            cardM.put("name", "充电卡");
            Map syM = new HashMap();
            syM.put("value", new BigDecimal(sys.get()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            syM.put("name", "系统");
            listMap.add(aliM);
            listMap.add(wechatM);
            listMap.add(cardM);
            listMap.add(syM);
        }
        map.put("pieData", listMap);
        return AjaxResult.success(map);
    }

    /**
     * 导出财务报表列表
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:export')")
    @Log(title = "财务报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChargerFinancial chargerFinancial)
    {
        List<ChargerFinancial> list = chargerFinancialService.selectChargerFinancialList(chargerFinancial);
        ExcelUtil<ChargerFinancial> util = new ExcelUtil<ChargerFinancial>(ChargerFinancial.class);
        util.exportExcel(response, list, "财务报表数据");
    }

    /**
     * 获取财务报表详细信息
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:query')")
    @GetMapping(value = "/{finanId}")
    public AjaxResult getInfo(@PathVariable("finanId") Long finanId)
    {
        return success(chargerFinancialService.selectChargerFinancialByFinanId(finanId));
    }

    /**
     * 新增财务报表
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:add')")
    @Log(title = "财务报表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChargerFinancial chargerFinancial)
    {
        return toAjax(chargerFinancialService.insertChargerFinancial(chargerFinancial));
    }

    /**
     * 修改财务报表
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:edit')")
    @Log(title = "财务报表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChargerFinancial chargerFinancial)
    {
        return toAjax(chargerFinancialService.updateChargerFinancial(chargerFinancial));
    }

    /**
     * 删除财务报表
     */
    @PreAuthorize("@ss.hasPermi('statistic:financial:remove')")
    @Log(title = "财务报表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{finanIds}")
    public AjaxResult remove(@PathVariable Long[] finanIds)
    {
        return toAjax(chargerFinancialService.deleteChargerFinancialByFinanIds(finanIds));
    }

}
